package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    //@RequiredPermission(name="权限列表",expression="permission:list")
    //处理查询所有
    @RequestMapping("list")
    public String list (Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "permission/list";//找视图
    }

    //去新增
    @RequestMapping("input")
    public String input (Model model, Long id) {
        if (id != null) {
            model.addAttribute("permission", permissionService.selectById(id));
        }
        return "/permission/input";//找视图

    }

    @RequestMapping("delete")
    public String delete (Long id) {
        if (id != null) {
            permissionService.deleteById(id);
        }
        return "redirect:/permission/list.do";//让浏览器重新发一次请求
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate (Permission permission) {
        if (permission.getId() == null) {
            permissionService.save(permission);
        } else {
            permissionService.updateById(permission);
        }

        return "redirect:/permission/list.do";//让浏览器重新发一次请求
    }

    //@RequiredPermission(name="权限加载",expression="permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload () {
        try {
            permissionService.reload();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "加载失败");
        }
    }

}
